package com.njlime.member.controller.sys;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.PageBean;
import com.njlime.core.sdk.wechat.user.UserLable;
import com.njlime.core.utils.DateUtil;
import com.njlime.core.utils.ExcelUtils;
import com.njlime.member.entiies.MemberNotice;
import com.njlime.member.entiies.TMember;
import com.njlime.member.entiies.TMemberGroup;
import com.njlime.member.service.sys.MemberGroupService;
import com.njlime.member.service.sys.MemberNoticeService;
import com.njlime.member.service.sys.MemberService;
import com.njlime.sys.security.util.UserUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "member")
public class MemberController  extends BaseController {

    @Resource
    private MemberService memberService;
    @Resource
    private MemberGroupService memberGroupService;
    @Resource
    private MemberNoticeService memberNoticeService;


    /**
     * 跳转热点管理页面
     * @return
     */
    @RequestMapping(value = "toMember")
    public ModelAndView roleManage(HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView("/sys/member/member");
        TMemberGroup info = new TMemberGroup();
      int unitId = UserUtils.getUser().getRootUnit().getId();


        info.setUnit_id(unitId);
        modelAndView.addObject("memberGroupList",memberGroupService.list(info));
        modelAndView.addObject("memberAllGroup",memberGroupService.getSumMember(info));

        return modelAndView;
    }


    /**
     * 跳转商品订单管理页面
     * @return
     */
    @RequestMapping(value = "reportView")
    public ModelAndView goodsOrderView(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("/sys/member/report");
        return modelAndView;
    }

    /**
     * 查询图片素材分组
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getGroupList", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getGroupList(PageBean pageBean, HttpServletRequest request ) {
        try {

            TMemberGroup info = new TMemberGroup();
            int unitId = UserUtils.getUser().getRootUnit().getId();


            info.setUnit_id(unitId);
            Map map = memberGroupService.getSumMember(info);
            List<TMemberGroup> list =memberGroupService.list(info);
            Map resMap = new HashMap();

            resMap.put("sumMap",map);
            resMap.put("list",list);

            return toJsonListData(resMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 分页列表
     * @param pageBean
     * @param member
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String list(PageBean pageBean, TMember member,String startDay,String endDay,String condition,String flag,String priceType,String orderType) {

         int unitId = UserUtils.getUser().getRootUnit().getId();
        if(startDay!=null &&!startDay.equals("")) {

            endDay = DateUtil.addDay( startDay.split("\\+/\\+")[1],1);
            startDay = startDay.split("\\+/\\+")[0];

        }
        member.setUnit_id(unitId);
        return toJsonListData(memberService.list(pageBean, member,startDay,endDay, condition, flag,priceType,orderType));
    }

    /**
     * 详情
     * @param id
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "info", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String info(String id, HttpServletRequest request, HttpServletResponse response) {
        try {
            return toJsonSuccessData(memberService.findOne(id));
        } catch (Exception e) {
            logger.error("", e);
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    /**
     * 通知列表
     * @param pageBean
     * @param id
     * @return
     */
    @RequestMapping(value = "noticeList", method = RequestMethod.GET)
    @ResponseBody
    public String noticeList(PageBean pageBean, Integer id) {
        MemberNotice memberNotice = new MemberNotice();
        memberNotice.setMember_id(id);
        return toJsonListData(memberNoticeService.list(pageBean, memberNotice));
    }

    /**
     * 修改用户分组
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "editMemberGroup", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String editMemberGroup(String ids,int groupId, HttpServletRequest request, HttpServletResponse response) {
        try {
           int unitId = UserUtils.getUser().getRootUnit().getId();

          List<TMember> list = memberService.selectUserGroupList(groupId,ids,unitId);
           UserLable.batchtagging(unitId,list,groupId);
            memberService.editMemberGroup(ids,groupId,unitId);
           return toJsonSuccessData();
        } catch (Exception e) {
            logger.error("", e);
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }
    /**
     * 編輯是否禁言
     *
     * @param info
     * @param request
     * @return
     */
    @RequestMapping(value = "editGag", method = RequestMethod.POST)
    @ResponseBody
    public String editGag(TMember info, HttpServletRequest request) {
     int unitId = UserUtils.getUser().getRootUnit().getId();

        info.setUnit_id(unitId);
        int count = memberService.updateGag(info);
        if (count > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "更新失败");
        }
    }



    /**
     * 編輯状态
     *
     * @param info
     * @param request
     * @return
     */
    @RequestMapping(value = "editStatus", method = RequestMethod.POST)
    @ResponseBody
    public String editStatus(TMember info, HttpServletRequest request) {
        info.setUnit_id(UserUtils.getUser().getRootUnit().getId());
        info.setManager_id(UserUtils.getUser().getId());
        info.setManager_udate_time(new Date());
        int count = memberService.updateStatus(info);
        if (count > 0) {
            return toJsonSuccessData();
        } else {
            return toJsonErrorData(ExceptionState.BUSINESS_EXCEPTION, "更新失败");
        }
    }

    @RequestMapping(value = "getReportList")
    @ResponseBody
    public String getReportList(PageBean pb,HttpServletRequest request,HttpServletResponse response,String  provinceId,String cityId,String  startDay,String endDay,String type) {
        logger.info("vip/getReportList");
        return toJsonListData(memberService.getReportList(pb, provinceId, startDay, endDay, cityId));
    }


    /**
     * 统计
     * @param pageBean
     * @param startTime
     * @param endTime
     * @param province_id
     * @param city_id
     * @return
     */
    @RequestMapping(value = "report")
    @ResponseBody
    public String report(PageBean pageBean,Date startTime, Date endTime, Integer province_id, Integer city_id){
        return toJsonListData(memberService.report(pageBean, getLoginUser().getUnit(), startTime, endTime, province_id, city_id, true));
    }


    @RequestMapping(value = "downMemberReport", method = RequestMethod.GET)
    public void downMemberReport(HttpServletResponse response, Date start_time, Date end_time, Integer city_id, Integer province_id) {
        String titleColumn[] = {"report_date","province_name","city_name","new_num","all_num","vip_num","active_num"};
        String titleName[] = {"日期","省份","区域","新增会员数","累计会员数","累计付费用户","活跃用户"};
        ExcelUtils.exportExcel(response, titleColumn, titleName, null, null,
                memberService.report(new PageBean(Integer.MAX_VALUE), getLoginUser().getUnit(), start_time, end_time, province_id, city_id, false).getMapList(),"会员统计表");
    }

}
